import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sprintf/sprintf.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/pages/tasks/component/advice_widget.dart';
import 'package:yt_devops/pages/tasks/component/basic_info_item_widget.dart';
import 'package:yt_devops/pages/tasks/component/descript_widget.dart';
import 'package:yt_devops/pages/tasks/component/pic_gallery_widget.dart';
import 'package:yt_devops/pages/tasks/component/title_widget.dart';
import 'package:yt_devops/pages/tasks/component/wrap_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/enums.dart';
import '../../../styles/styles.dart';
import 'report_task_detail_controller.dart';

class ReportTaskDetailPage extends GetView<ReportTaskDetailController> {
  const ReportTaskDetailPage({Key? key}) : super(key: key);

  @override
  ReportTaskDetailController get controller =>
      Get.put(ReportTaskDetailController(), tag: Get.parameters['id']);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget.defaultAppBar(ResKey.workOrderDetails.tr),
      body: GetBuilder<ReportTaskDetailController>(
        tag: Get.parameters['id'],
        init: controller,
        builder: (controller) => ListView(padding: EdgeInsets.zero, children: [
          WrapWidget(
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              TitleWidget(
                ResKey.baseInfo.tr,
              ),
              ...controller.model.baseInfos
                  .map((e) => BasicInfoItemWidget(e))
                  .toList(),
              SizedBox(
                height: 8.h,
              ),

              // 问题描述
              ...{
                dividerWidget(),
                TitleWidget(
                  ResKey.problemStastic.tr,
                  titleStyle: TextStyles.f16.mediumStyle,
                ),
                descriptWidget()
              },

              // 附件
              if (controller.model.pics != null) ...{
                dividerWidget(),
                PicGalleryWidget(controller.model.pics!),
              },

              //当前处理人
              if (controller.model.handlerName != null) ...[
                dividerWidget(),
                TitleWidget(ResKey.currentHandleBy.tr,
                    titleStyle: TextStyles.f16.mediumStyle,
                    value: controller.model.handlerName),
              ],
              // 当前处理人手机号
              if (controller.model.phone != null) ...[
                dividerWidget(),
                TitleWidget(
                  ResKey.phone.tr,
                  titleStyle: TextStyles.f16.mediumStyle,
                  value: controller.model.phone,
                ),
              ],
            ]),
          ),
          //任务进度
          WrapWidget(
            padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              TitleWidget(ResKey.schedule.tr,
                  titleStyle: TextStyles.f16.mediumStyle),
              for (var i = 0; i < controller.model.tasilInfos.length; i++)
                BrnStepLine(
                  lineWidth: 1,
                  //非第一个是灰色
                  isGrey: controller.model.tasilInfos[i].id == null,
                  //最后一个的线条为透明色 做到不显示的效果
                  lineColor: i == controller.model.tasilInfos.length - 1
                      ? Colors.transparent
                      : controller.model.tasilInfos[i].id != null &&
                              controller.model.tasilInfos[i + 1].id != null
                          ? Colour.primaryColor
                          : const Color(0xffeeeeee),
                  contentWidget: Container(
                    padding: EdgeInsets.only(left: 10.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('${controller.model.tasilInfos[i].name}',
                            style:
                                TextStyles.f14.boldStyle.copyWith(height: 0.9)),
                        Padding(
                          padding: const EdgeInsets.only(top: 12, bottom: 20),
                          child: Text(
                              controller.model.tasilInfos[i].processorName !=
                                      null
                                  ? '${controller.model.tasilInfos[i].processorName}  ${controller.model.tasilInfos[i].createTime}'
                                  : '',
                              style: TextStyles.lf14.copyWith(height: 0.9)),
                        ),
                      ],
                    ),
                  ),
                ),
            ]),
          ),

          Gaps.vGap10,

          //按钮

          if (controller.model.status ==
              ReportTaskStatusType.waitingForProcessing)
            Container(
              color: Colour.white,
              padding: EdgeInsets.fromLTRB(0, 0, 0, 20.h),
              child: YtBrnBottomButtonPanel(
                secondaryButtonBorder:
                    Border.all(color: Colour.diabledButtoBgColor),
                mainButtonName: sprintf(ResKey.commonCreateSentence.tr,
                    [ResKey.workOrderMaintenanceOrder.tr]),
                secondaryButtonName: ResKey.finished.tr,
                mainButtonOnTap: controller.toCreateRepairOrder,
                secondaryButtonOnTap: controller.toFinish,
              ),
            )
          else if (controller.model.status ==
                  ReportTaskStatusType.underRepair ||
              controller.model.status == ReportTaskStatusType.repairCompleted)
            Container(
              color: Colour.white,
              padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 27.h),
              child: YtButton(
                text: ResKey.viewFaultRepairOrder.tr,
                onPress: () => controller.checkMaintainOrder(),
              ),
            )
        ]),
      ),
    );
  }

  Widget dividerWidget() {
    return const Divider(
      color: Color(0xFFEEEEEE),
      height: 1,
    );
  }

  descriptWidget() {
    Widget textWidget;
    if (controller.model.discroption.length < 50) {
      textWidget = Text(
        controller.model.discroption,
        style: TextStyles.f14,
      );
    } else {
      textWidget = BrnExpandableText(
        text: controller.model.discroption,
        textStyle: TextStyles.f14,
        maxLines: 4,
      );
    }
    return Padding(
      padding: const EdgeInsets.only(
        bottom: 16,
      ),
      child: textWidget,
    );
  }
}
